package com.opgea.token.web.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.opgea.token.util.FacesUtil;


public class LoginCheckFilter implements Filter {

    @Override
    public void destroy() {
	// TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
	    FilterChain chain) throws IOException, ServletException {
	HttpServletRequest request = (HttpServletRequest) req;
	HttpServletResponse response = (HttpServletResponse) res;
	HttpSession session = request.getSession();
	if (session == null) {
	    System.out.println("Context Name: "
		    + FacesUtil.getExternalContext().getContextName());
	    System.out.println("RequestContext Path: "
		    + FacesUtil.getExternalContext().getRequestContextPath());
	    response.sendRedirect(FacesUtil.getExternalContext()
		    .getContextName() + "/login/login.xhtml");
	} else {
	    chain.doFilter(req, res);
	}

    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
	// TODO Auto-generated method stub

    }
}
